// 文件：src/main.js（应用入口）
import { createApp, vaporInteropPlugin } from "vue"
import App from "./App.vue";
import { router } from "./router";
import { createPinia, setActivePinia } from 'pinia';
import { i18n } from './i18n';
import "./assets/tailwind.css";
import { loadBuiltinPlugins, loadRuntimePlugins } from "./services/pluginManager";
import { initTheme } from './assets/theme.js';
import { commandService } from './services/commandService';
import { useCommandPaletteStore } from './stores/commandPalette';
import { initAiToolsStore } from './stores/aiTools';
import { DiagnosticManager } from './services/diagnosticManager';


// Apply theme ASAP to avoid initial flash/mismatch
try { initTheme(); } catch {}

const app = createApp(App);
app.use(vaporInteropPlugin);

const pinia = createPinia();
app.use(pinia);

// Allow using stores outside of components (e.g., in adapters)
try { setActivePinia(pinia); } catch (e) { /* no-op */ }

// 初始化 AI 工具调度 Store（注册内置工具等）
try { initAiToolsStore(); } catch (e) { /* no-op */ }

app.use(router);
app.use(i18n);

// 显式挂载 $t，避免某些编译/插件模式下的全局注入失效
try {
  if (!app.config.globalProperties.$t && i18n?.global?.t) {
    app.config.globalProperties.$t = i18n.global.t.bind(i18n.global);
  }
} catch (e) {
  // no-op
}


app.mount("#app");

// 在应用挂载后加载插件和注册命令
loadBuiltinPlugins().then(async () => {
    // Load runtime plugins after built-in plugins
    await loadRuntimePlugins();
    
    // Initialize diagnostic manager to start code analysis
    try {
        DiagnosticManager.instance;
        console.log('[Main] Diagnostic manager initialized');
    } catch (error) {
        console.warn('[Main] Failed to initialize diagnostic manager:', error);
    }
    
    const commandPaletteStore = useCommandPaletteStore();
    commandService.register(
      'workbench.action.showCommands',
      'Command Palette',
      () => {
        commandPaletteStore.open();
      }
    );
});
